"""扫描任务API"""
from typing import List
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session

from app.database import get_db
from app.services.scan_task_service import ScanTaskService
from app.schemas.scan_task import ScanTaskResponse
from app.schemas.common import ResponseBase, PaginatedResponse
from app.services.scan_task_service import ScanTaskService

router = APIRouter()


@router.get("", response_model=PaginatedResponse[ScanTaskResponse])
async def get_scan_tasks(
    skip: int = 0,
    limit: int = 100,
    db: Session = Depends(get_db),
):
    """获取扫描任务列表"""
    service = ScanTaskService(db)
    tasks = service.get_all(skip=skip, limit=limit)
    total = service.repository.count()

    return PaginatedResponse(
        code=200,
        message="success",
        data=[ScanTaskResponse.from_orm(task) for task in tasks],
        total=total,
        skip=skip,
        limit=limit,
    )


@router.get("/{task_id}", response_model=ResponseBase)
async def get_scan_task(
    task_id: int,
    db: Session = Depends(get_db),
):
    """获取扫描任务详情"""
    service = ScanTaskService(db)
    task = service.get_by_id(task_id)

    if not task:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"扫描任务 {task_id} 不存在",
        )

    return ResponseBase(
        code=200,
        message="success",
        data=ScanTaskResponse.from_orm(task),
    )

